//
//  AiScreenFunc.swift
//  HLDesktop
//
//  Created by 子凡 on 2025/4/10.
//

import UIKit

//aiScreen独有方法
class AiScreenFunc: NSObject {
    //解析标题得到搜索关键词(兼容支付宝、微信小程序等)
    class func splitTitleToGetKeyword(title:String,urlScheme:String)->String?{
    //约定格式：
    //一般AppStore里:正标题 - 副标题, App内部功能： "功能名·App名称",   系统设置："设置 ⇾ 功能页名"
        if title.isEmpty{
            return title
        }
        
        if title.contains("-"){//AppStore类型
            let titleArr:[String] = title.components(separatedBy: "-")
            return titleArr[0].trimSpace()
        }else if title.contains("·"){//小程序功能类型
            let titleArr = title.components(separatedBy: "·")
            return titleArr[1].trimSpace()
        }else if title.contains("⇾"){
    //设置项，不进行关键词搜索
            return nil
        }
        else{
            if let strTitle = getUrlschemeTitle(urlScheme: urlScheme) {
                return strTitle.trimSpace()
            }else{
                return title.trimSpace()
            }
        }
    }
    
    //解析App标题为正副标题
    class func splitToTitleAndSub(title:String)->[String]{
        let separteArr:[String] = ["-","·","⇾"]
        var separteChar = "-"
        for perChar in separteArr{
            if title.contains(perChar){
                separteChar = perChar
                break
            }
        }
        
        let titleArr = title.components(separatedBy: separteChar)
        return titleArr
    }

    //解析url命令得到搜索关键词(兼容支付宝、微信小程序等)
    class func getUrlschemeTitle(urlScheme:String)->String?{
    //alipay-支付宝，weixin-微信/dingtalk-钉钉/wxwork-企业微信/mqqapi-QQ/tim-TIM/openapp.jdmobile-京东/feishu-飞书
    //App-Prefs更不需
        if urlScheme.hasPrefix("App-Prefs"){
            return "设置"
        }
        let lowUrl = urlScheme.lowercased()
        if lowUrl.hasPrefix("alipay"){
            return "支付宝"
        }else if lowUrl.hasPrefix("weixin") || lowUrl.hasPrefix("wechat"){
            return "微信"
        }else if lowUrl.hasPrefix("dingtalk"){
            return "钉钉"
        }else if lowUrl.hasPrefix("wxwork"){
            return "企业微信"
        }else if lowUrl.hasPrefix("mqqapi"){
            return "QQ"
        }else if lowUrl.hasPrefix("tim://"){
            return "TIM"
        }else if lowUrl.hasPrefix("openapp.jdmobile"){
            return "京东"
        }else if lowUrl.hasPrefix("feishu"){
            return "飞书"
        }else{
            if let data = UserDefaults.standard.value(forKey: "aiScreen.basic.title.forurl"){
                if let dictData = data as? NSDictionary {
                    if let strData = dictData[urlScheme] as? String {
                        return strData
                    }
                }
            }
        }
        return nil
    }
    
    //MARK: - 显示广告View
    class func appStart(){
        let homeVC = ViewController()
        homeVC.preInitView() //调用主要为触发ViewController的viewDidLoad,从而可以先进行initView，但不appearView
//        let adView = AdView(frame: CGRect(x: 0, y: 0, width: screen_W, height: screen_H))
//注意：此种方式创建，removeFromSuperview后，AdView不会deinit。应改为下面var optional变量方式：
        var adView:AdView? = AdView(frame: CGRect(x: 0, y: 0, width: screen_W, height: screen_H))
        adView?.tapBlock = {
            if let tmpView = adView{
                for perView in tmpView.subviews{
                    perView.removeFromSuperview()
                }
            }
            adView?.removeFromSuperview()
            adView = nil
//显示RootVC，可以
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else  {return}
            if let window = windowScene.windows.first{ //=SceneDelegate里的window
//取window方式一：
                window.rootViewController = homeVC
            }else if let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate {
//方式二：也可以直接引用SceneDelegate的对象window
                sceneDelegate.window?.rootViewController = homeVC
            }
        }
    }
    
//MARK: - 显示FPS监测
    class func showFPS(){
    }
}

